package com.cliang.servlet;

import com.alibaba.fastjson.JSON;
import com.cliang.entity.BlogUser;
import com.cliang.entity.SessionUser;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class GetSessionUser extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("utf-8");
       resp.setCharacterEncoding("utf-8");
       resp.setContentType("text/html;charset=UTF-8");

        HttpSession session=req.getSession();
        PrintWriter printWriter=resp.getWriter();
        BlogUser blogUser= (BlogUser) session.getAttribute("blogUser");
        SessionUser sessionUser=new SessionUser(blogUser.getBuId().toString(),blogUser.getBuNickname(),blogUser.getBuName(),blogUser.getBuPicture(),blogUser.getBuEmail(),blogUser.getBuTime());
//        sessionUser.setId(blogUser.getBuId().toString());
//        sessionUser.setNickName(blogUser.getBuNickname());
//        sessionUser.setName(blogUser.getBuName());
//        sessionUser.setPicUrl(blogUser.getBuPicture());
//        sessionUser.setEmail(blogUser.getBuEmail());
//        sessionUser.setRegTime(blogUser.getBuTime());

        String jsonObject= JSON.toJSONString(sessionUser);
        printWriter.write(jsonObject);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
